$(function () {
    $("#birthday").datepicker({
        dateFormat: "yy-mm-dd",
        // timeFormat: "HH:mm:ss"
    })
    $("#joinPartyDate").datepicker({
        dateFormat: "yy-mm-dd",
        // timeFormat: "HH:mm:ss"
    })
    $("#normalPartyDate").datepicker({
        dateFormat: "yy-mm-dd",
        // timeFormat: "HH:mm:ss"
    })
    $("#participationDate").datepicker({
        dateFormat: "yy-mm-dd",
        // timeFormat: "HH:mm:ss"
    })

    $("#jqGrid").jqGrid({
        url: baseURL + 'deptmanage/staffinfo/list',
        datatype: "json",
        colModel: [
            {label: 'userseq', name: 'userseq', index: 'userseq', width: 50, key: true},
            {label: '人员姓名', name: 'username', index: 'username', width: 80},
            {label: '性别', name: 'gender', index: 'gender', width: 80 ,formatter:selectByDictCode},
            {label: '出生日期', name: 'birthday', index: 'birthday', width: 80},
            {
                label: '证件类型', name: 'idType', index: 'id_type', width: 80, formatter: function (value, row, index) {
                    var retHTML = '';
                    $.ajax({
                        type: 'get',
                        url: '',
                        data: {value: value, type: ''},
                        async: false,
                        success: function (data) {
                            retHTML = data.name;
                        }
                    })
                    return retHTML;
                }
            },
            {label: '证件号码', name: 'idNo', index: 'id_no', width: 80},
            {label: '婚姻状态（已婚，未婚，丧偶，离婚，再婚）', name: 'marriage', index: 'marriage', width: 80 ,formatter:selectByDictCode},
            {label: '民族', name: 'nation', index: 'nation', width: 80},
            {label: '籍贯', name: 'nativePlace', index: 'native_place', width: 80},
            {label: '居住地', name: 'birthPlace', index: 'birth_place', width: 80},
            {label: '入职日期', name: 'joinPartyDate', index: 'join_party_date', width: 80},
            {label: '转正日期', name: 'normalPartyDate', index: 'normal_party_date', width: 80},
            {label: '使用工资', name: 'testSalary', index: 'test_salary', width: 80},
            {label: '转正工资', name: 'normalSalary', index: 'normal_salary', width: 80},
            {label: '社保状态（新开，续缴，跨地转移，不缴）', name: 'socialInsuranceStatus', index: 'social_insurance_status', width: 80 ,formatter:selectByDictCode},
            {label: '公积金（新开，转移，不缴）', name: 'accumulationFund', index: 'accumulation_fund', width: 80 ,formatter:selectByDictCode},
            {label: '参加工作日期', name: 'participationDate', index: 'participation_date', width: 80},
            {label: '健康状况', name: 'healthy', index: 'healthy', width: 80},
            {label: '学位', name: 'academicDegree', index: 'academic_degree', width: 80},
            {label: '学历', name: 'educationLevel', index: 'education_level', width: 80},
            {label: '政治面貌', name: 'political', index: 'political', width: 80},
            {label: '职务级别', name: 'jobLevel', index: 'job_level', width: 80 ,formatter:selectByDictCode},
            {label: '人员状态（在职，实习，离职，试用）', name: 'userstatus', index: 'userstatus', width: 80 ,formatter:selectByDictCode},
            {label: '紧急联系人', name: 'emergencyContact', index: 'emergency_contact', width: 80},
            {label: '紧急联系人电话', name: 'emergencyContactTel', index: 'emergency_contact_tel', width: 80},
            {label: '创建日期', name: 'createDate', index: 'create_date', width: 80},
            {label: '创建人', name: 'createUser', index: 'create_user', width: 80},
            {label: '修改日期', name: 'updateTime', index: 'update_time', width: 80},
            {label: '修改人', name: 'updateUser', index: 'update_user', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        staffInfo: {},
        q: {
            key: null
        },
        isAdd: true,
        gender:[],
        marriage:[],
        socialInsuranceStatus:[],
        accumulationFund:[],
        jobLevel:[],
        userstatus:[]
    },
    mounted: function () {
        this.gender = selectDictMap('gender');
        this.marriage = selectDictMap('marriage');
        this.socialInsuranceStatus = selectDictMap('socialInsuranceStatus');
        this.accumulationFund = selectDictMap('accumulationFund');
        this.jobLevel = selectDictMap('jobLevel');
        this.userstatus = selectDictMap('userstatus');
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.staffInfo = {};
            this.isAdd = true;
        },
        update: function (event) {
            var userseq = getSelectedRow();
            if (userseq == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            this.isAdd = false;

            vm.getInfo(userseq)
        },
        saveOrUpdate: function (event) {
            var url = this.isAdd ? "deptmanage/staffinfo/save" : "deptmanage/staffinfo/update";
            vm.staffInfo.birthday = $("#birthday").val();
            vm.staffInfo.joinPartyDate = $("#joinPartyDate").val();
            vm.staffInfo.normalPartyDate = $("#normalPartyDate").val();
            vm.staffInfo.participationDate = $("#participationDate").val();

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.staffInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var userseqs = getSelectedRows();
            if (userseqs == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "deptmanage/staffinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userseqs),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (userseq) {
            if (!userseq) {
                alert('人员编码不存在')
                return;
            }
            $.get(baseURL + "deptmanage/staffinfo/info/" + userseq, function (r) {
                vm.staffInfo = r.staffInfo;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'key': vm.q.key
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});